package com.kcnet.qk.controller.hdyh;

import android.util.Log;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;

import com.kcnet.qk.app.AppContext;
import com.kcnet.qk.constant.Const;
import com.kcnet.qk.constant.StringConst;
import com.kcnet.qk.controller.BasePeopleController;
import com.kcnet.qk.controller.addfriend.AddFriendAdoptCTR;
import com.kcnet.qk.util.FBStringUtil;
import com.kcnet.qk.util.NodeUtil;
import com.kcnet.qk.util.PreferenceHelper;
import com.kcnet.qk.util.ToastUtil;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import cn.jesse.nativelogger.NLogger;

import static com.kcnet.qk.constant.Const.LIST_VIEW;
import static com.kcnet.qk.util.NodeUtil.findNodeByTextAndPerformClick;

/**
 * Created by Administrator on 2018/4/27.
 */

public class HdTimeLineZans  extends BasePeopleController {

    private static String TAG = "时间线点赞";
    private int maxAddCount = 3; //最大添加10人
    private int currentAddCount = 0; //当前任务添加人数

    private static HdTimeLineZans mRongCloudInstance;


    /**
     * 获取RongCloud 实例。
     *
     * @return RongCloud。
     */
    public static HdTimeLineZans getInstance() {
        if (mRongCloudInstance == null) {
            synchronized (HdTimeLineZans.class) {
                if (mRongCloudInstance == null) {
                    mRongCloudInstance = new HdTimeLineZans();
                }
            }
        }

        return mRongCloudInstance;
    }




    @Override
    public void doTask(AccessibilityEvent event) {
        mClassName = (String) event.getClassName();

        mType = event.getEventType();

        if (mType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED && mClassName.equals(Const.FbUI.FBLauncherUI)) {
            NLogger.d(TAG, "1、点击到对应模块");
            //tagTextClick("第1/4个功能选项");
            tagTextClick(FBStringUtil.getString(StringConst.TAB_DTXX));
        }

        else if (mType == AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED && mClassName.equals("android.support.v7.widget.RecyclerView")
                || mType == AccessibilityEvent.TYPE_VIEW_SCROLLED && mClassName.equals("android.support.v7.widget.RecyclerView")) {
            NLogger.d(TAG, "2、开始自动点赞");
            beginAddFriendAdopt();
        }else if(mType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED && mClassName.equals("X.2Bh")){ //弹出对话框自动点击确认
            // mService.execShellCmd("input tap 197 141"); //随便模拟一次点击 消失对话框
            if(!findNodeByTextAndPerformClick(getRoot(), "确定")){
                findNodeByTextAndPerformClick(getRoot(), "确认");
            }
        }
    }


    /**
     * 執行添加推薦的好友
     */
    private void beginAddFriendAdopt(){
        AccessibilityNodeInfo scrllorNodeInfo = null;
        AccessibilityNodeInfo root = getRoot();
        if (root != null) {
            synchronized (mLock) {
                List<AccessibilityNodeInfo> scrllorNodeInfos = NodeUtil.traverseNodeByClassList(root, "android.support.v7.widget.RecyclerView");
                if(scrllorNodeInfos.size() > 0){
                    scrllorNodeInfo = scrllorNodeInfos.get(0);
                }
                if(scrllorNodeInfo == null){
                   // ToastUtil.showToast(AppContext.getInstance().getContext(),"scrllorNodeInfo listView null");
                    return;
                }

                for(int q=0;q<scrllorNodeInfo.getChildCount();q++){

                    if(currentAddCount  == maxAddCount){
                        currentAddCount = 0;
                        stopTask();
                        //停止任务
                        break;
                    }

                    AccessibilityNodeInfo as = scrllorNodeInfo.getChild(q);
                    List<AccessibilityNodeInfo> list = as.findAccessibilityNodeInfosByText(FBStringUtil.getString(StringConst.HDYH_ZAN));
                    if(!list.isEmpty()){
                        AccessibilityNodeInfo zanNode = list.get(0);
                        NLogger.d(TAG,"点击了点赞按钮   "+   zanNode.getContentDescription().toString());
                        if(!zanNode.getContentDescription().toString().equals(FBStringUtil.getString(StringConst.HDYH_ZAN_YAX))){
                                sleepTimedDefault();
                                currentAddCount++;
                                NLogger.d(TAG,"点击了点赞按钮"+   NodeUtil.performClick(zanNode));
                                break;
                        }

                    }


                    if (q == scrllorNodeInfo.getChildCount() - 1) {
                        if (scrllorNodeInfo != null) {
                          //  execShellCmd("input swipe 500 500 500 300");
                           // SWIPE_DOWN_AND_UPClick();
                            NodeUtil.performScroller(scrllorNodeInfo);
                            break;
                        }
                    }
                }

            }
        }
    }

    public void setParms(int maxAddCount,int minAddTime,int maxAddTime){
      //  stopTask();
        EventBus.getDefault().post(new Boolean(true));
        PreferenceHelper.putBoolean(Const.PREF_KEY_STOP_AUTO_FLAG, true);
        this.maxAddCount = maxAddCount;
        this.minAddTime = minAddTime;
        this.maxAddTime = maxAddTime;
        this.currentAddCount = 0;
    }



}
